package com.tongsys.server.run;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.junit.Test;
import org.springframework.stereotype.Component;

import java.sql.Types;
import java.util.Collections;

@Component
public class GeneratorCode {

    @Test
    public  void code() {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/tongsys?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("yyfei") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .commentDate("yyyy-MM-dd")
                            .outputDir("E:\\workspace\\tongsys\\src\\main\\java"); // 指定输出目录, 注意这点的生成目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.tongsys.server") // 设置父包名
                         .moduleName(null) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "E:\\workspace\\tongsys\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("permission") // 设置需要生成的表名
                            .entityBuilder().enableLombok().enableTableFieldAnnotation().enableFileOverride()
                            .controllerBuilder().enableHyphenStyle().enableRestStyle().enableFileOverride()
                            .mapperBuilder().enableMapperAnnotation().enableFileOverride();
                })
                .execute();
    }



}
